package calao.storage;

/** A record field.
 * 
 * @param <V> the type of values stored in the field.
 */
public class Field<V> 
{
		
	public final static FieldType<String> STRING = 
		new FieldType<String>(String.class, FieldType.Enum.STRING);
	
	public final static FieldType<Integer> INTEGER = 
		new FieldType<Integer>(Integer.class, FieldType.Enum.INTEGER);
	
	public final static FieldType<Short> SHORT = 
		new FieldType<Short>(Short.class, FieldType.Enum.SHORT);

	public final static FieldType<Long> LONG = 
		new FieldType<Long>(Long.class, FieldType.Enum.LONG);
	
	public final static FieldType<Double> DOUBLE = 
		new FieldType<Double>(Double.class, FieldType.Enum.DOUBLE);
	
	public final static FieldType<Binary> BINARY = 
		new FieldType<Binary>(Binary.class, FieldType.Enum.BINARY);

	final Table table;
	final int index;
	final String name;
	final FieldType type;
	final boolean multiValued;
		
	Field(Table table, String name, FieldType<? super V> type)
	{
		this.table = table;
		this.index = table.getFieldCount();
		this.name = name;
		this.type = type;
		this.multiValued = false;
	}

	Field(Table table, String name, FieldType<? super V> type, boolean multiValued)
	{
		this.table = table;
		this.index = table.getFieldCount();
		this.name = name;
		this.type = type;
		this.multiValued = multiValued;
	}
	
	/** Gets the type of this field.
	 */
	public FieldType getType()
	{
		return type;
	}
	
	/** Gets the name of this field.
	 */
	public String getName()
	{
		return name;
	}
	
	/** Gets the table holding this field.
	 */
	public Table getTable()
	{
		return table;
	}
	
	/** Checks whether this field accepts multiple values.
	 */
	public boolean isMultiValued()
	{
		return multiValued;
	}
	
	/** Gets the position of this field in the table.
	 */
	public int getPosition()
	{
		return index;
	}
	
	@Override
	public String toString()
	{
		return table.name+"#"+name;
	}
			
}
